//导入路由相关函数
import { createRouter,createWebHashHistory } from "vue-router";
//定义路由数组
const routes =[
    {
        path:'/',
        name:'登录',
        component:()=>import("../views/login/index.vue")
    },
    {
        path:'/home',
        name:'首页',
        component:()=>import("../views/index/index.vue"),
        meta:{
            requireAuth:true
        },
        children:[
            {
                path:"",
                meta:{
                    title:'系统管理'
                },
                children:[
                    {
                        path:'/user',
                        component:()=>import("../views/user/index.vue"),
                    meta: {
                        title:'用户管理'
                    }
                    }
                ]
            }
        ]
    }
]
//创建路由实例
const router = createRouter({
    history:createWebHashHistory(),
    routes:routes
})
// 全局路由导航守卫
router.beforeEach((to,from,next)=>{
    const requireAuth = to.matched.some(record=>record.meta.requireAuth)
    //检查用户是否登录
    const isLogin = !!localStorage.getItem("user")
    if(requireAuth && ! isLogin){
        next("/")
    }else{
        next()
    }
})

//导出
export default router